package se.webbzon.oschi01.tools.mapper;

import java.util.ArrayList;
import java.util.Collection;

import se.webbzon.oschi01.item.ItemEntry;
import se.webbzon.oschi01.item.ItemEntryMetric;
import se.webbzon.oschi01.metric.XWangMetric;

public class MapperSearcher {
	
	private ItemEntryMetric metric;
	
	private double tolerance;
	
	private int searchLimit;
	
	public MapperSearcher() {
		this(new ItemEntryMetric(new XWangMetric()),0.3,25);
	}
	
	public MapperSearcher(ItemEntryMetric metric, double tolerance, int searchLimit) {
		this.metric = metric;
		this.tolerance = tolerance;
		this.searchLimit = searchLimit;
	}

	public ItemEntryMetric getMetric() {
		return metric;
	}

	public void setMetric(ItemEntryMetric metric) {
		this.metric = metric;
	}

	public double getTolerance() {
		return tolerance;
	}

	public boolean setTolerance(double tolerance) {
		if (tolerance < 0 || tolerance > 1)
			return false;
		this.tolerance = tolerance;
		return true;
	}

	public int getSearchLimit() {
		return searchLimit;
	}

	public void setSearchLimit(int searchLimit) {
		this.searchLimit = searchLimit;
	}
	
	public Collection<ItemEntry> search(MapperModel model, ItemEntry oldEntry, String searchString) {
		if (searchString.length() >= 3) {
			if (searchString.matches("\\w*")) {
				searchString = ".*" + searchString + ".*";
			}
			return model.getAvailableItems().match(searchString,searchLimit);
		} else if (oldEntry != null) {
			return model.getAvailableItems().similar(oldEntry,metric,searchLimit,tolerance);
		} else
			return new ArrayList<ItemEntry>();
	}

}
